package com.angel.web.app;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.SMSUtils;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzDoctor;
import com.angel.health.BuzDoctorWork;
import com.angel.health.BuzDoctorWorkLog;
import com.angel.health.SMS;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzDoctorProvider;
import com.angel.provider.health.BuzDoctorWorkLogProvider;
import com.angel.provider.health.BuzDoctorWorkProvider;
import com.github.pagehelper.StringUtil;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年11月16日 下午6:10:59 
* 类说明  预约医师服务
*/
@Controller
@RequestMapping("app/doctorWork")
public class AppDoctorWorkController {

	@Autowired
	private BuzDoctorWorkProvider bdwProvider;
	@Autowired
	private BuzDoctorWorkLogProvider bdwlProvider;
	@Autowired
	private BuzDoctorProvider bdProvider;
	@Autowired
	private BuzCustomerProvider bcProvider;
	/**
	 * 医生被预约记录
	 * @param bdw
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="workDateAll", produces = "application/json; charset=utf-8")
	public String workDateAll(BuzDoctorWork bdw){
		if(bdw.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String, Object>> bdwList = bdwProvider.findWorkAll(bdw);
		return JsonUtil.toJson(new JsonEntity("200", bdwList));
	}
	/**
	 * 医师工作日期
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="workDate", produces = "application/json; charset=utf-8")
	public String workDate(BuzDoctorWork bdw){
		if(bdw.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String, Object>> bdwList = bdwProvider.findWorkDate(bdw);
		return JsonUtil.toJson(new JsonEntity("200", bdwList));
	}
	
	/**
	 * 未过时的预约
	 * @param bdwl
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="selectLog", produces = "application/json; charset=utf-8")
	public String selectLog(BuzDoctorWorkLog bdwl){
		if(bdwl.getCustomerId() == null && bdwl.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String, Object>> bdwList = bdwlProvider.selectLog(bdwl);
		return JsonUtil.toJson(new JsonEntity("200", bdwList));
	}
	/**
	 * 医师工作时间查询
	 * @param bdwl
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="workTime", produces = "application/json; charset=utf-8")
	public String workTime(BuzDoctorWorkLog bdwl){
		if(bdwl.getDoctorId() == null && bdwl.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String, Object>> bdwlList = bdwlProvider.findAll(bdwl);
		return JsonUtil.toJson(new JsonEntity("200", bdwlList));
	}
	/**
	 * 客户预约医师
	 * @param bdwl
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="arrange", produces = "application/json; charset=utf-8")
	public String arrange(BuzDoctorWorkLog bdwl){
		if(bdwl.getDoctorId() == null || StringUtil.isEmpty(bdwl.getWorkDate()) || bdwl.getCustomerId() == null || StringUtil.isEmpty(bdwl.getArrangeTime())){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String, Object>> bdwlList = bdwlProvider.findAll(bdwl);
		if(bdwlList.size() > 0){
			return JsonUtil.toJson(new JsonEntity("-1", "该时间点已被预约"));
		}
		int insert = bdwlProvider.insert(bdwl);
		if(insert > 0){
			BuzDoctor bd = new BuzDoctor();
			bd.setId(bdwl.getDoctorId());
			bd = bdProvider.findById(bd);
			BuzCustomer bc = new BuzCustomer();
			bc.setId(bdwl.getCustomerId());
			bc = bcProvider.findById(bc);
			String single = Constants.SEND_SMS_SINGLE;
			String content = "恭喜，您已成功预约在线就诊服务，医师"+bd.getName()+"将于"+bdwl.getWorkDate()+" "+bdwl.getArrangeTime()+"对您进行在线服务，谢谢【天使健康】";
			String memPhone = bc.getMemPhone();
			JsonEntity json = SMSUtils.send(new SMS(bdwl.getId().toString(),memPhone,content,bdwl.getId().toString(),""),single);
			return JsonUtil.toJson(new JsonEntity("200", "预约成功"));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "预约失败"));
	}
	
	/**
	 * 预约过医师的所有客户
	 */
	@ResponseBody
	@RequestMapping(value="docCus", produces = "application/json; charset=utf-8")
	public String docCus(BuzDoctorWorkLog bdwl){
		if(bdwl.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		List<Map<String,Object>> list = bcProvider.docCus(bdwl);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
}
